//Domain               = Packages.de.novanic.eventservice.client.event.domain.Domain;
//DomainFactory        = Packages.de.novanic.eventservice.client.event.domain.DomainFactory;
//EventRegistry        = Packages.de.novanic.eventservice.service.registry.EventRegistry;
//EventRegistryFactory = Packages.de.novanic.eventservice.service.registry.EventRegistryFactory;

function main( json, ui, hh ) {
  
 var json = ui.getQueryParameters().getFirst("json");
  if ( json == null ){
        var ex = "{ exception: { message: 'no param provided'} }";
        return ex;
  }
 // var jsonObject = new JSONObject( json );
   var transactions = new JSONArray( json );
 
  var obj1 = transactions.get(0);
  var stored = getTransactions(obj1);
  return storeTransaction(transactions, stored);
  
}

function storeTransaction(transactions,stored){
  
  var transactionIn = new TransactionEntity();
  var transactionOut = new TransactionEntity();
  var jsonIn = null;
  var jsonOut = null;
  var storedIn = null;
  var storedOut = null;
  var jsonStored = null;
  var json = transactions.get(0);

  if ( json.get('subType').equals('in')){
        jsonIn = transactions.get(0);
        jsonOut = transactions.get(1);
             
   } else {
        jsonIn = transactions.get(1);
        jsonOut = transactions.get(0);
    
   }
  
  
  if ( stored.length() == 0 ){
      
    jsonIn.copyTo(transactionIn);
    jsonOut.copyTo(transactionOut); 
    
     EntityManager.createTransaction( transactionIn );
     EntityManager.createTransaction( transactionOut );
    
     return "created 2 transactions";
    
  } 
  
  if ( stored.length() == 1 ){
    
     jsonStored = stored.get(0);
   
     if ( jsonStored.get('subType').equals('in')){
         storedIn = stored.get(0)
         storedIn.copyTo(transactionIn);
         
        createTransaction(jsonOut);
        EntityManager.storeTransaction( transactionIn );
    
        return "created=" + jsonOut + " stored=" + transactionIn;
       
     } else {
         storedOut = stored.get(0)
         storedOut.copyTo(transactionOut);
         createTransaction(jsonIn);
         EntityManager.storeTransaction( transactionOut );
         return "created=" + jsonIn + " stored=" + transactionOut;
     
     }
   }
  
  if ( stored.length() == 2 ){

     jsonStored = stored.get(0);
   
     if ( jsonStored.get('subType').equals('in')){
         storedIn = stored.get(0);
         storedOut = stored.get(1);
     } else {
         storedOut = stored.get(0);
         storedIn = stored.get(1);
     }
    
    storedIn.put('lat',jsonIn.get('lat'));
    storedIn.put('lng',jsonIn.get('lng'));
    storedIn.put('longDate',jsonIn.get('longDate'));
    
    storedOut.put('lat',jsonOut.get('lat'));
    storedOut.put('lng',jsonOut.get('lng'));
    storedOut.put('longDate',jsonOut.get('longDate'));
    
   
    storedIn.copyTo(transactionIn);
    storedOut.copyTo(transactionOut);
    
    EntityManager.storeTransaction( transactionIn );
    EntityManager.storeTransaction( transactionOut );
   
    return "stored=" + transactionIn + " stored=" + transactionOut;
 
   }
  
  if ( stored.length() > 2 )
    var ex = "{ exception: { message: 'found more than 2 transactions'} }";
        return ex;

  } 
 

function getTransactions(jsonObject) {

  var records = new JSONArray();

  var cmd = "select t from TransactionEntity t where t.refId=:refId";
  
  
    EntityManager.find(  
    cmd,  
    jsonObject,
     function(result) {
      
      for( var i = 0; i < result.size() ; ++i ) {
        
        records.put( JSONObject.wrap(result.get(i)) );
      }
      
    });
    
    var result = new JSONObject();
    
    result.put( "records", records );
    

    return records ;

}


function createTransaction( o ) {
  var t = new TransactionEntity();
  
  o.copyTo( t );
  
  EntityManager.createTransaction( t );
  
  

}
/*
function sendEvent(){

  var domain = DomainFactory.getDomain("BADGEONLINE");

  var reg = EventRegistryFactory.getInstance();
  var re = reg.getEventRegistry();
      
  var o = new JSONObject();
  o.put("echo", "test");
  re.addEvent(domain, new JSONEvent( o.toString() ) );

  
}
*/